<template>
	<view class="height-100 bg-white">
		<view class="bg-white padding radius-xl margin main flex-direction-column justify-between"
			style="margin-top: -40rpx; height: calc(100vh - 240rpx);">
			<view class="">
				<view class="" style="margin-top: 100rpx;">
					<image :src="'../../static/images/logo.png'" class="cu-photo round" mode="aspectFit"></image>
				</view>
				<view class="nh">
					您好！
				</view>
				<view class="hy margin-bottom-xl">
					欢迎使用【杨杨批发】
				</view>
				<view class="padding-tb border-bottom margin-top flex-direction-row">
					<image :src="'../../static/images/sjh.png'" class="cu-avatar-xs round" mode="aspectFit"></image>
					<input type="number" v-model="phone" placeholder="请输入手机号" />
				</view>
				<view class="padding-tb border-bottom margin-top flex-direction-row">
					<view class="flex-direction-row">
						<image :src="'../../static/images/yzm.png'" class="cu-avatar-xs round" mode="aspectFit"></image>
						<input class="flex-sub" type="number" v-model="code" placeholder="请输入验证码" />
					</view>
					<template v-if="codeState">
						<text class="text-df color-gray">{{countdown}}s</text>
					</template>
					<template v-else>
						<text class="button-captcha" @click="getCode">获取短信验证码</text>
					</template>
				</view>
				<view class="padding-tb-sm radius-sl bg-df1 text-center margin-tb-xl" @click="login">
					<text class="text-lg color-white">登录</text>
				</view>
				<view class="flex-direction-row align-start">
					<view class="" style="margin-top: 3rpx;" @click="select">
						<template v-if="isSelect">
							<text class="text-df color-red iconfont padding-xs">&#xe647;</text>
						</template>
						<template v-else>
							<text class="text-df color-black-q iconfont padding-xs">&#xe600;</text>
						</template>
					</view>
					<view class="flex-sub">
						<text class="text-sm color-black-q">未注册的手机号验证通过后将自动注册帐号，我已阅读并同意签署</text>
						<text class="text-sm color-black-b text-bold" @click="protocol('user')">《1延天龄用户协议》</text>
						<text class="text-sm color-black-b text-bold" @click="protocol('privacy')">《延天龄隐私政策》</text>
					</view>
				</view>
			</view>
			<uni-popup ref="popup" type="center">
				<view class="bg-white padding-xl margin-xl radius-xl flex-direction-column" style="margin-bottom: 100rpx;">
					<text class="text-xl text-bold text-center padding-tb-xs">隐私保护提示</text>
					<text
						class="text-smd margin-top line-height-lg">欢迎使用延天龄小程序！延天龄非常重视用户的个人信息和隐私保护。在您使用延天龄小程序，请认真阅读《用户协议》和《隐私政策》，如您同意并接受全部条款可开始使用我们的服务。</text>
					<text class="text-df padding-tb-sm bg-df radius-sl color-white margin-top-xl text-center"
						@click="popup(true)">同意</text>
					<text class="text-df color-black-q margin-top text-center" @click="popup(false)">不同意</text>
				</view>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import {
		getPhoneNumber,
		getUserinfo,
	} from "@/common/user.js"
	export default {
		onLoad() {
			// 登录缓存则跳转
			const userinfo = uni.getStorageSync('userinfo');
			const token = uni.getStorageSync('token');
			console.log(token);
			console.log(userinfo);
			if (userinfo && token) {
				uni.switchTab({  // 专用于跳转 TabBar 页面
				    url: "/pages/index/index"
				})
			}
		},
		data() {
			return {
				phone: '',
				password: '',
				code: '',
				codeState: false,
				codingText: '获取短信验证码',
				countdown: 60,
				isSelect: true,
				isSelect: false,
				isPhoneNumberMode: false
			}
		},
		methods: {
			select() {
				this.isSelect = !this.isSelect
			},
			// 获取验证码
			async getCode() {
				// 获取验证码
				if (this.codeState) {
					return false
				};
				if (this.phone === '') {
					uni.showToast({
						title: '手机号不能为空',
						icon: 'none',
					});
					return;
				}
				if (!RegExp(/^1[3456789]\d{9}$/).test(this.phone)) {
					uni.showToast({
						title: '手机号格式错误',
						icon: 'none',
					});
					return;
				}
				// 1 生成验证码
				this.codeState = true;
				const res = await uni.$request.get('/doctorApi/sendSms/' + this.phone);
				// this.codeFocus = true;
				this.countdown = 59;
				let Time = setInterval(() => {
					if (this.countdown === 0) {
						this.codeState = false;
						this.countdown = 60;
						clearInterval(Time)
					} else {
						this.countdown--;
					}
				}, 1000);
				if (res.msg === '1') {
					uni.showModal({
						title: '提示',
						content: res.data,
						showCancel: false
					})
				}
			},
			protocol(type) {
				uni.navigateTo({
					url: '/pages/login/protocol?type=' + type
				});
			},
			select() {
				this.isSelect = !this.isSelect
				// this.$refs.popup.open();
			},
			popup(type) {
				this.isSelect = type;
				this.$refs.popup.close();
				// 如果同意隐私保护，且是非手机快捷登录方式，直接走登录逻辑
				if (type && this.isPhoneNumberMode === false) {
					this.loginRequest();
				}
			},
			login() {
				if (this.phone === '') {
					uni.showModal({
						title: "请输入手机号"
					})
					return;
				}
				if (!RegExp(/^1[3456789]\d{9}$/).test(this.phone)) {
					uni.showToast({
						title: '手机号格式错误',
						icon: 'none',
					});
					return;
				}
				if (this.code === '') {
					uni.showModal({
						title: "请输入验证码"
					})
					return;
				}
				if (this.isSelect) {
					this.loginRequest('code');
				} else {
					// 弹出隐私保护提示
					this.$refs.popup.open();
				}
			},
			// 获取用户信息
			async getUserinfo(type = '') {
				let res = await getUserinfo();
				console.log('userinfo: ', res);
				if (res.code === 200) {
					uni.showToast({
						title: '登录成功',
						icon: 'none'
					});
					// 缓存用户数据
					uni.setStorageSync('userinfo', res.data);
				} else {
					uni.showModal({
						content: '获取用户信息失败',
						showCancel: false
					});
				}
			},
			// 登录请求
			async loginRequest(type) {
				let data = {
					mobile: this.phone,
					code: this.code,
					roleType: 1
				}
				if (type) data.type = type;
				const res = await uni.$request.post('/doctorApi/login', data);
				console.log('loginRequest res: ', res);
				if (res.code === 200) {
					// 缓存用户token
					uni.setStorageSync('token', res.token);
					this.getUserinfo();
					setTimeout(function() {
						uni.navigateTo({
							url: "/pages/index/index"
						})
						// 通知其它页面登录成功，更新页面数据
						// uni.$emit('loginStatusChange', {
						// 	login: 'login'
						// });
					}, 800);
				} else {
					uni.showModal({
						content: res.msg,
						showCancel: false
					});
				}
			}
		}
	}
</script>

<style scoped>
	.main {}

	.line {
		width: 80rpx;
		height: 1rpx;
		margin: 0 20rpx;
		background-color: #999;
	}

	.login-button {
		background-color: #d93722;
		height: 88rpx;
		border-radius: 88rpx;
	}
	.nh {
		height: 86rpx;
		font-family: Source Han Sans, Source Han Sans;
		font-weight: 400;
		font-size: 60rpx;
		color: #222222;
		line-height: 86rpx;
		text-align: left;
		font-style: normal;
		text-transform: none;
		margin-top: 20rpx;
		margin-top: 3rpx;
	}
	.hy {
		height: 58rpx;
		font-family: Source Han Sans, Source Han Sans;
		font-weight: 500;
		font-size: 40rpx;
		color: #222222;
		line-height: 58rpx;
		text-align: left;
		font-style: normal;
		text-transform: none;
		margin-top: 3rpx;
	}
</style>